import {Component, EventEmitter, Input, OnInit, Output} from '@angular/core';
import {ExecActionMainService} from "../../plugin/exec-action-main.service";
import {ExecActionInterface} from "../../plugin/exec-action-interface";
import {RuleExpression} from "../../model/rule-expression";

@Component({
  selector: 'app-rule-gui',
  templateUrl: './rule-gui.component.html',
  styleUrls: ['./rule-gui.component.css']
})
export class RuleGuiComponent implements OnInit {

  actionObj: ExecActionInterface | undefined
  @Input()
  iconType?:string|null
  @Input()
  ruleData:RuleExpression ={action:'',expression:{},children:[]}

  @Output("remove")
  removeEvent:EventEmitter<void> = new EventEmitter<void>()
  execList: ExecActionInterface[]
  constructor(private execMain:ExecActionMainService) {
    this.execList = execMain.execList

  }

  ngOnInit(): void {
    console.log("input =>",this.ruleData)
    this.changeAction()

  }



  public changeAction(){
    this.execList.forEach(it=>{
      if (it.code === this.ruleData.action){
        this.actionObj = it;
      }
    })
  }

  public addChild(){
    if (this.ruleData.children == null){
      this.ruleData.children=[]
    }
    this.ruleData.children.push({
      action:"",
      expression:""
    })
  }
  public sendRemoveEvent(){
    this.removeEvent.emit()
  }
  public removeChild(item:RuleExpression){
    this.ruleData.children?.splice(this.ruleData.children?.indexOf(item),1)
  }
}
